#include "system.h"
#include "can.h"
#include "tim.h"
#include "rcc.h"
#include "lin.h"
#include "led.h"
#include "iwdg.h"



u8 buf[8]={0x03,0x00,0x80,0x00,0x00,0x00,0xff,0x00};
u8 send_flag;
LinTxMsg linReMsg1= {.ID = 0x29,
    .Data[0] = 0xA1,
    .Data[1] = 0xA2,
    .Data[2] = 0xA3,
    .Data[3] = 0xA4,
    .Data[4] = 0xA5,
    .Data[5] = 0xA6,
    .Data[6] = 0xA7,
    .Data[7] = 0xA8,
		.CheckSum_mode = CheckSum_Classic,
    .Length = 8,
};
LinRxMsg linReMsg2= {.ID = 0x09,
    .Data[0] = 0xA0,
    .Data[1] = 0xA0,
    .Data[2] = 0xA0,
    .Data[3] = 0xA0,
    .Data[4] = 0xA0,
    .Data[5] = 0xA0,
    .Data[6] = 0xA0,
    .Data[7] = 0xA0,
		.CheckSum_mode = CheckSum_Classic,
    .Length = 8,
};

void System_Init(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 
	init_RCC();
	Init_LedLamp();

	get_Config_From_Flash();	

	IWDG_Init();
}

void Task_Schedule(void)
{
	switch(mode)
	{

		//Blow is Configration Mode
		//0x56:return CANID_Public
		case 0x57:send_CANID_Public();break;
		//0x58:return CANID_Filter_Max
		case 0x59:send_CANID_Filter_Max();break;
		//0x5a:return CANID_Filter_Min
		case 0x5b:send_CANID_Filter_Min();break;
		//0x5c:return CAN2_Speed
		case 0x5d:send_CAN2_Speed();break;
		//0x5e:return Version
		case 0x5f:send_Version();break;
		
		case 0x74:set_LIN_Speed();break;
//		case 0x75:set_LIN_Mode();break;
//		case 0x76:set_LIN_CheckSum_Mode();break;
//		case 0x77:set_LIN_Data_Length();break;
		case 0x78:set_FeedBack();break;
		case 0x79:set_CANID_Filter_Min();break;
		case 0x7a:set_CANID_Filter_Max();break;
		case 0x7b:Set_CAN1_Speed();break;
		case 0x7c:Set_CAN2_Speed();break;
		case 0x7d:set_CANID_Public();break;
		case 0x7e:set_CANID_Private();break;
		case 0x7f:Reboot();break;
	}
	if(send_flag==1)
	{
		Can2_Send_Msg(0x200,buf,8);
		send_flag=0;
	}
	
	Lin_Send_Msg(&linReMsg1);
	
	LEN_Master_read(&linReMsg2);

	IWDG_Feed();
}



